import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        list: [],
        inputValue: 'aaaa',
        nettId: 5
    },

    mutations: {
        INITLIST(state, list) {
            state.list = list
        },
        SETINPUTVALUE(state, value) {
            state.inputValue = value
        },
        ADDITEM(state) {
            const obj = {
                id: state.nettId,
                info: state.inputValue.trim(),
                done: false
            }
            state.list.push(obj)
            state.nettId++
                state.inputValue = ''
        },
        DELETEITEM(state, id) {
            const i = state.list.findIndex(x => x.id === id)
            if (i !== -1) {
                state.list.splice(i, 1)
            }
        },
        CHANGESTATUS(state, param) {
            const i = state.list.findIndex(x => x.id === param.id)
            if (i !== -1) {
                state.list[i].done = param.status
            }
        },
        CLEANDONE(state) {
            state.list = state.list.filter(x => x.done === false)
        }
    },

    actions: {
        async getList(context) {
            let { data } = await axios.get('/list.json')
            context.commit('INITLIST', data)
        }
    },

    getters: {
        unDoneLength(state) {
            return state.list.filter(x => x.done === false).length
        }
    }
})